package com.sj.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sj.common.bean.LocalUser;
import com.sj.common.dto.collect.CollectParam;
import com.sj.common.exception.CustomException;
import com.sj.common.lang.ApiResult;
import com.sj.common.lang.Const;
import com.sj.common.lang.RedisConst;
import com.sj.entity.Collect;
import com.sj.entity.User;
import com.sj.interceptor.AuthCheck;
import com.sj.service.CollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 收藏关注表 前端控制器
 * </p>
 *
 * @author 孙峻
 * @since 2022-03-09
 */
@RestController
@RequestMapping("/collect")
@Api(tags = "收藏关注模块")
public class CollectController {

    @Autowired
    private CollectService collectService;

    @PutMapping("/add")
    @ApiOperation("添加收藏关注")
    @AuthCheck
    @CacheEvict(cacheNames = {"article-detail" , "article-num-data", "fix-type", "article-list"}, allEntries = true, keyGenerator = RedisConst.REDIS_SJ_KEY_GENERATOR, condition = "#result.code == 200")
    public ApiResult addCollect(@RequestBody CollectParam collectParam) {
        User user = LocalUser.getUser();
        Collect collect = new Collect();
        BeanUtil.copyProperties(collectParam, collect, "typeId");
        if (Const.ARTICLE.equals(collectParam.getType())) {
            collect.setArticleId(collectParam.getTypeId());
        } else if (Const.AUTHOR.equals(collectParam.getType())) {
            collect.setAuthorId(collectParam.getTypeId());
        } else {
            throw new CustomException("文章类型错误");
        }
        collect.setUserId(user.getId());
        // 首先判断是否收藏文章或关注作者
        Boolean isCollect = collectService.checkIsCollect(user.getId(), collectParam.getTypeId(), collectParam.getType());
        if (isCollect) {
            // 取消收藏关注
            LambdaQueryWrapper<Collect> query = Wrappers.<Collect>lambdaQuery().eq(Collect::getUserId, collect.getUserId());
            if (Const.ARTICLE.equals(collectParam.getType())) {
                query.eq(Collect::getArticleId, collect.getArticleId());
            } else if (Const.AUTHOR.equals(collectParam.getType())) {
                query.eq(Collect::getAuthorId, collect.getAuthorId());
            }
            collectService.remove(query);
            return ApiResult.success(Const.ARTICLE.equals(collectParam.getType()) ? "已取消收藏" : "已取消关注");
        }
        // 如果没有
        boolean result = collectService.save(collect);
        if (result) {
            return ApiResult.success(Const.ARTICLE.equals(collectParam.getType()) ? "收藏成功" : "关注成功");
        }
        return ApiResult.error("关注或收藏失败");
    }

}
